//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "private.h"
#include "win32.h"

DWORD s_threadDescrIndex = (DWORD)-1;

EXTERN Aura_ECode DestroyElAuraAsDuty();

static BOOL AuraContext_Initialize()
{
    s_pDoAtExceptionalExitFunc = NULL;
    s_pProcessManager = NULL;
    s_hExceptionalExitSurveillant = NULL;

    s_threadDescrIndex = TlsAlloc();

    if ((DWORD)-1 == s_threadDescrIndex) {
        return FALSE;
    }

    return TRUE;
}

static BOOL AuraContext_Destroy()
{
    DestroyElAuraAsDuty();
    if ((DWORD)-1 != s_threadDescrIndex) {
        TlsFree(s_threadDescrIndex);
    }
    return TRUE;
}

EXTERN DWORD g_dwStartTickCount;
EXTERN ULONGLONG g_lStartIdleCount;

EXTERN_C BOOL WINAPI DllMain(HANDLE hModule,
    DWORD dwReason,
    LPVOID lpReserved)
{
    BOOL bSucc = TRUE;
    FILETIME idletime;

    switch (dwReason) {
        case DLL_PROCESS_ATTACH:
            // DisableThreadLibraryCalls(hModule);
            bSucc = AuraContext_Initialize();
            if (!bSucc) {
                break;
            }

            //Get system start tick count
            g_dwStartTickCount = GetTickCount();

            //Get system idle count
            bSucc = GetSystemTimes(&idletime, NULL, NULL);
            if (!bSucc) {
                break;
            }
            g_lStartIdleCount = idletime.dwHighDateTime;
            g_lStartIdleCount = g_lStartIdleCount << 32;
            g_lStartIdleCount |= idletime.dwLowDateTime;

            break;
        case DLL_PROCESS_DETACH:
            bSucc = AuraContext_Destroy();
            break;
    }
    return bSucc;
}
